package com.example.demo.controller;

import com.example.demo.entity.blog;
import com.example.demo.entity.collect;
import com.example.demo.entity.favorite;
import com.example.demo.mapper.blogMapper;
import com.example.demo.mapper.collectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
public class collectController {
    @Autowired
    collectMapper collectMapper;
    @Autowired
    blogMapper blogMapper;

    //查询收藏夹
    @GetMapping("/selectfavoriteBymailbox/{mailbox}")
    public String selectfavoriteBymailbox(@PathVariable String mailbox) {
        return collectMapper.selectfavoriteBymailbox(mailbox).toString();
    }

    //查询收藏根据收藏夹id
    @GetMapping("/selectcollectByfavoritrid/{id}")
    public String selectcollectByfavoritrid(@PathVariable int id) {
        List<collect> list = collectMapper.selectcollectByfavoritrid(id);
        for (int i = 0; i < list.size(); i++) {
            blog blog = blogMapper.selectblogByid(list.get(i).getBlogid());
            list.get(i).setBlog(blog);
        }
        return list.toString();
    }

    //创建收藏夹
    @GetMapping("/insertfavorite/{users}/{name}/{miaoshu}")
    public String insertfavorite(@PathVariable String users, @PathVariable String name, @PathVariable String miaoshu) {
        favorite favorite = new favorite();
        favorite.setMiaoshu(miaoshu);
        favorite.setName(name);
        favorite.setUsers(users);
        collectMapper.insertfavorite(favorite);
        return favorite.toString();
    }

    //删除收藏夹
    @GetMapping("/deletefavorite/{id}")
    public void deletefavorite(@PathVariable int id) {
        collectMapper.deletefavorite(id);
        collectMapper.delectcollectbyfavoriteid(id);
    }

    //删除收藏
    @GetMapping("/delectcollectbyid/{id}")
    public void delectcollectbyid(@PathVariable int id) {
        collectMapper.delectcollectbyid(id);
    }

    //添加收藏
    @GetMapping("/insertcollect/{favoriteid}/{blogid}")
    public String insertcollect(@PathVariable int favoriteid, @PathVariable int blogid) {
        collect c = new collect();
        c.setBlogid(blogid);
        c.setFavoriteid(favoriteid);
        collectMapper.insertcollect(c);
        return c.toString();
    }

    //查询是否收藏,这是用在blog详情页面的是否收藏查询的
    @GetMapping("/selectbymailbox/{mailbox}/{id}")
    public String select(@PathVariable String mailbox, @PathVariable int id) {
        List<favorite> favoriteList = collectMapper.selectfavoriteBymailbox(mailbox);
        for (int i = 0; i < favoriteList.size(); i++) {
            List<collect> collectList = collectMapper.selectcollectByfavoritrid(favoriteList.get(i).getId());
            for (int j = 0; j < collectList.size(); j++) {
                if (collectList.get(j).getBlogid() == id) {
                    return collectList.get(j).getId()+"";
                }
            }
        }
        return "no";
    }
    //修改收藏夹根据id
    @GetMapping("/updatefavorite/{id}/{name}/{miaoshu}")
    public String updatefavorite(@PathVariable int id,@PathVariable String name,@PathVariable String miaoshu){
        favorite f=new favorite();
        f.setMiaoshu(miaoshu);
        f.setName(name);
        f.setId(id);
        collectMapper.updatefavorite(f);
        return f.toString();
    }

}
